using MultiAgentsClient.Service.WorkflowEngine.BaseNodes;
using MultiAgentsShared;
using Newtonsoft.Json.Linq;

namespace MultiAgentsClient.Service.WorkflowEngine.BuiltInNodes;


/// <summary>
/// This node is a trigger node, not meant to have any logic
/// </summary>

public class NodeGitLabRequest(string nodeId) : NodeRequestToService(nodeId)
{
    public int projectId = 0;
    public int mergeRequestId = 0;
    public string TokenEnvName = "";
    public string repositoryUri = "";
}

public class NodeGitLabResponse(string nodeId) : NodeResponseFromService(nodeId)
{
    public string PullRequestContent = "";
}

[NodeGraphPortDef(0, 1)]
public class GitlabNode : ServiceDependentNode<NodeGitLabRequest, NodeGitLabResponse>
{
    [NodeGraphParam] public string RepositoryURL = "";
    [NodeGraphParam] public string TokenEnvName = "";
    [NodeGraphParam] public bool ShouldAskMe = true;



    // Extract required fields
    [NodeGraphOutput] private string title;
    [NodeGraphOutput] private string description;
    [NodeGraphOutput] private string state;
    [NodeGraphOutput] private string source_branch;
    [NodeGraphOutput] private string target_branch;

    // Extract changes
    [NodeGraphOutput] private List<string> changes;
    [NodeGraphOutput] private string PRNodeResult;


    private Dictionary<string, object> GitlabParam = [];
    public GitlabNode() : base() { }

    protected override void SendRequestToService()
    {
        NodeGitLabRequest nodeLlmRequest = new(Id)
        {
            projectId = Convert.ToInt32(GitlabParam["projectId"]),
            mergeRequestId = Convert.ToInt32(GitlabParam["mergeRequestId"]),
            repositoryUri = (string)GitlabParam["repositoryUri"],
            TokenEnvName = TokenEnvName,
        };

        EventBus.Publish(nodeLlmRequest);
    }

    protected override NodeOutcome ServiceResponseHandler(NodeGitLabResponse cmd)
    {
        var jsonObject = JObject.Parse(cmd.PullRequestContent);

        try
        {
            // Extract required fields
            title = (string)jsonObject["title"];
            description = (string)jsonObject["description"];
            state = (string)jsonObject["state"];
            source_branch = (string)jsonObject["source_branch"];
            target_branch = (string)jsonObject["target_branch"];

            // Extract changes
            JArray changesArray = (JArray)jsonObject["changes"];
            changes = changesArray.Select(c => (string)c["diff"]).ToList();

            PRNodeResult = "true";
            runtimeContext.SetVar(Id, () => PRNodeResult);

            runtimeContext.SetVar(Id, () => title);
            runtimeContext.SetVar(Id, () => description);
            runtimeContext.SetVar(Id, () => state);
            runtimeContext.SetVar(Id, () => source_branch);
            runtimeContext.SetVar(Id, () => target_branch);
            runtimeContext.SetVar(Id, () => changes);

            return new NodeOutcome(NodeOutcomeStatus.Success, 0);
        }
        catch (Exception e)
        {
            PRNodeResult = e.Message;
            runtimeContext.SetVar(Id, () => PRNodeResult);

            return new NodeOutcome(NodeOutcomeStatus.Failure, 0);
        }
    }
    // projectId, mergeRequestId, repositoryUri, sourceBranch, targetBranch, title, description
    public void PassParams(Dictionary<string, object> gitlabParam)
    {
        GitlabParam = gitlabParam;
    }
}
